package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-12 15:26
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.MusicMapper;
import com.datang.pet.mapper.MusicSearchRecordMapper;
import com.datang.pet.mapperExtend.MusicMapperExtend;
import com.datang.pet.pojo.Music;
import com.datang.pet.pojo.MusicExample;
import com.datang.pet.pojo.MusicSearchRecord;
import com.datang.pet.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicMapper musicMapper;

    @Autowired
    private MusicMapperExtend musicMapperExtend;

    @Autowired
    private MusicSearchRecordMapper musicSearchRecordMapper;

    /*
     * @author 顶风少年
     * @date 2019/6/12 15:31
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<Music>
     * @notify 获取推荐音乐
     * version 1.0
     */
    public List<Music> getRecommendMusicList() throws Exception {

        try {
            MusicExample musicExample = new MusicExample();
            MusicExample.Criteria criteria = musicExample.createCriteria();
            criteria.andIsRecommendEqualTo("1");
            List<Music> musicList = musicMapper.selectByExample(musicExample);
            return musicList;
        } catch (Exception e) {
            throw new Exception("获取推荐音乐失败");
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/6/12 15:37
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param pageNumber
     * @result java.util.List<Music>
     * @notify 获取音乐列表
     * version 1.0
     */
    public List<Music> getMusicList(int pageNumber) throws Exception {
        pageNumber = (pageNumber - 1) * 20;
        try {
            List<Music> musicList = musicMapperExtend.getMusicList(pageNumber);
            return musicList;
        } catch (Exception e) {
            throw new Exception("获取音乐失败");
        }

    }

    /*
     * @author 顶风少年
     * @date 2019/6/12 15:31
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param music
     * @result void
     * @notify 添加音乐
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void addMusic(Music music) throws Exception {

        try {
            musicMapper.insert(music);
        } catch (Exception e) {
            throw new Exception("添加音乐失败");
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/6/12 17:55
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param keyword
     * @param pageNumber
     * @result java.util.List<Music>
     * @notify 搜索音乐
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public List<Music> searchMusic(String userId, String keyword) throws Exception {

        //md5解密userId
        try {
            userId = MD5.decryptData(userId);
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }


        List<Music> musicList = new ArrayList<>();
        try{
            musicList = musicMapperExtend.searchMusic(keyword);
        }catch (Exception e){
            throw new Exception("搜索音乐出错");
        }


        //添加搜索记录
        MusicSearchRecord musicSearchRecord = new MusicSearchRecord();
        musicSearchRecord.setId(StringUtils.getId());
        musicSearchRecord.setUserId(userId);
        musicSearchRecord.setKeyword(keyword);
        musicSearchRecord.setDate(new Date());
        //如果查询不到
        if (musicList.isEmpty()) {
            musicSearchRecord.setNotHave("1");
        } else {
            musicSearchRecord.setNotHave("0");
        }
        try {
            musicSearchRecordMapper.insert(musicSearchRecord);
        } catch (Exception e) {
            throw new Exception("添加搜索记录失败");
        }


        return musicList;
    }
}
